// pages/Home/Library/Library.js
const app = getApp();
//设置成点击展开的样式
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    img: '',
    keyword: '',
    startpageindex: 1,
    boolList: [],
    bookData: null,
    preindex: 0,
    isbtn: true
  },
  preserach() {
    this.setData({
      startpageindex: this.data.startpageindex > 1 ? this.data.startpageindex - 1 : '0'
    }, () => {
      this.search();
    })
  },
  nextsearch() {
    this.setData({
      startpageindex: this.data.startpageindex + 1
    }, () => {
      console.log(this.data.startpageindex)
      this.search();
    })
  },
  getkeyword(e) {
    //获取关键词
    this.setData({ keyword: e.detail.value })
  },
  getDetail(e) {
    var that = this;
    var blist = that.data.bookList;
    var prehides = "bookList[" + this.data.preindex + "].hide";
    that.setData({
      [prehides]: true
    })
    //console.log(e)
    wx.showLoading({
      title: '详细信息',
    })
    //获取相信信息
    var index = e.currentTarget.dataset.index;
    var url = e.currentTarget.dataset.url;
    wx.cloud.callFunction({
      name: 'server', data: { url: url }
      , success(_res) {
        var bookInfo = {};
        var repx = /<table.*?>[\s\S]*?<\/table>/;
        var bookInfoString = _res.result.match(repx);
        repx = /<h2>.*?<\/h2>/;
        bookInfo.name = bookInfoString[0].match(repx)[0].replace("<h2>", "").replace("</h2>", "");
        repx = /<tr><td>.*<\/td><\/tr>/g;
        var bookInfoArray = [];
        bookInfoString[0].match(repx).map(function (value, index) {
          bookInfoArray.push(value.replace("<tr><td>", "").replace("</td></tr>", ""));
          return value;
        })
        repx = /<li>[\s\S]*?<\/li>/;
        bookInfoString = _res.result.match(repx);
        repx = /<li>[\s\S]*?<\/li>/;
        repx = /<p.*>.*<\/p>/g;
        bookInfoString[0].match(repx).map(function (value, index) {
          bookInfoArray.push(value.replace(/<p.*?>/, "").replace("</p>", ""));
          return value;
        })
        bookInfo.bookInfoArray = bookInfoArray;
        //bookInfo.imgUrl = this.getImgUrl();
        var hides = "bookList[" + index + "].hide";
        that.setData({
          bookData: bookInfo.bookInfoArray,
          [hides]: false,
          preindex: index
        }, () => {
          wx.hideLoading();
          //console.log(that.data.bookData)
        })
        //console.log(bookInfo)
      }
    })
  },
  search(e) {
    var that = this;
    var q = this.data.keyword;
    var startpage = this.data.startpageindex;
    // console.log('begin searh')
    wx.showLoading({
      title: '请求数据中~',
    })
    wx.cloud.callFunction({
      name: 'server',
      data: {
        url: encodeURI('http://interlib.sdust.edu.cn/opac/m/search?curlibcode=01000&q=' + q + '&page=' + startpage)
      },
      success(res) {
        // console.log(res)
        var bookList = [];
        var repx = /<li onclick.*?>[\s\S]*?<\/li>/g;
        res.result.match(repx).forEach(function (value, index, array) {
          var listObject = {};
          var infoBookFour = [];
          repx = /<em>.*<\/em>/g;

          value.match(repx).map(function (valueBook, indexBook, arrayBook) {
            valueBook = valueBook.replace("<em>", "");
            valueBook = valueBook.replace("</em>", "");
            infoBookFour.push(valueBook);
            return value;
          })
          listObject.infoList = infoBookFour;
          repx = /isbn="(.)*"/g;
          var serialInfo = value.match(repx)[0].split("  ");
          listObject.isbn = serialInfo[0].split('"')[1];
          listObject.bookrecno = serialInfo[1].split('"')[1];
          repx = /javascript:bookDetail(.)*;/g;
          listObject.url = "http://interlib.sdust.edu.cn" + value.match(repx)[0].split("'")[1].split(";")[0];
          listObject.hide = true;
          bookList.push(listObject);
        })
        console.log(bookList)
        that.setData({
          'bookList': bookList,
          isbtn: false
        }, () => {
          //setData可以进行一些特殊渲染
        })
        wx.hideLoading();
      }, fail(res) {
        wx.hideLoading();
        wx.showModal({
          title: '图书馆服务器已关闭',
          content: '',
          showCancel: false,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ img: app.globalData.imgList.libimg })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})